using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WeaponListDisplay : MonoBehaviour {
	
	public GameObject itemDisplayTemplate;
	
	List<GameObject> itemDisplays;
	
	public UITable table;
	
	public bool toggled = false;
	
	WeaponItem selectedWeapon;

	// Use this for initialization
	void Start () {
		
		itemDisplayTemplate.SetActiveRecursively(false);
		
		itemDisplays = new List<GameObject>();
		
		UIButtonMessage message = gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "ToggleWeaponDisplays";
		
		table.repositionNow = true;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void ToggleWeaponDisplays()
	{
		if(!toggled)
		{
			itemDisplayTemplate.SetActiveRecursively(true);
			
			List<WeaponItem> sortedList = SheetManager.activeCharacter.weapons;
			sortedList.Sort((w1,w2) => w1.name.CompareTo(w2.name));
			
			GameObject go = new GameObject();
			go.transform.parent = transform.parent;
			go.transform.localPosition = Vector3.zero;
			go.transform.localScale = Vector3.one;
			go.name = itemDisplayTemplate.name;
			itemDisplays.Add(go);
			
			go = NGUITools.AddButton(go, 24, "Add Weapon or Attack");
			UIButtonMessage message = go.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "EditWeaponList";
			go.transform.localPosition = Vector3.right * ((BoxCollider)go.collider).size.x/2;
			
			foreach(WeaponItem weapon in sortedList)
			{
				if(weapon != null)
				{
					go = Instantiate(itemDisplayTemplate) as GameObject;
					go.transform.parent = transform.parent;
					go.transform.localPosition = itemDisplayTemplate.transform.localPosition;
					go.transform.localScale = itemDisplayTemplate.transform.localScale;
					go.name += weapon.name;
					
					WeaponListDisplayTemplate template = go.GetComponent<WeaponListDisplayTemplate>();
					template.wname.text = weapon.name;
					template.info.text = weapon.weaponDescription;
					template.wname.maxHeight = 40;
					template.weapon = weapon;
					
					message = template.button.AddComponent<UIButtonMessage>();
					message.target = gameObject;
					message.functionName = "AttackRoll";
					
					message = go.AddComponent<UIButtonMessage>();
					message.target = gameObject;
					message.functionName = "DisplayWeaponOptions";
					
					itemDisplays.Add(go);
				}
			}
			
			itemDisplayTemplate.SetActiveRecursively(false); 
			
			table.repositionNow = true;
			toggled = true;
		}
		else
		{
			foreach(GameObject go in itemDisplays)
			{
				Destroy(go);
			}
			itemDisplays.Clear();
			table.repositionNow = true;
			toggled = false;
		}
	}
	
	void EditWeaponList()
	{
		BrowserManager.CreateItemsBrowser(gameObject, "AddWeapon", new List<string>(), true);
	}
	
	void AddWeapon(List<string> addedWeapons)
	{
		foreach(string wn in addedWeapons)
		{
			string weaponName = wn.TrimEnd(new char[]{'^'});
			
			if(SheetManager.activeCharacter.weapons.Find(w => w.name == weaponName) != null)
			{
				SheetManager.activeCharacter.weapons.Find(w => w.name == weaponName).quantity++;
			}
			else
			{
				WeaponItem w = WeaponManager.Load(weaponName);
				if(w != null)
					SheetManager.activeCharacter.weapons.Add(w);
			}
		}
		
		SheetManager.SaveActiveCharacter();
		ToggleWeaponDisplays();
		ToggleWeaponDisplays();
	}
	
	void AttackRoll(GameObject target)
	{
		selectedWeapon = target.transform.parent.GetComponent<WeaponListDisplayTemplate>().weapon;
		
		List<string> options = new List<string>{"Attack","Full Attack","Damage","Critical Damage"};
		
		if(SheetManager.activeCharacter.sneakDice > 0)
		{
			options.Add("Sneak Damage");
			options.Add("Crit. Sneak Damage");
		}
		
		if(MathParser.Evaluate("Monk_LVL") > 0)
		{
			options.Add("Flurry of Blows");
		}
		
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "Action", false, options)
		};
		
		GameObject buttonWindow;
		
		HelperFunctions.ButtonWindow("What Kind of Roll?", descVar, "RollType", gameObject, out buttonWindow);
	}
	
	void RollType(string type)
	{
		switch(type)
		{
		case "Attack":
			selectedWeapon.RollAttack(false);
			break;
		case "Full Attack":
			selectedWeapon.RollAttack(true);
			break;
		case "Flurry of Blows":
			selectedWeapon.RollAttack(true, true);
			break;
		case "Damage":
			selectedWeapon.RollDamage();
			break;
		case "Critical Damage":
			selectedWeapon.RollDamage(true);
			break;
		case "Sneak Damage":
			selectedWeapon.RollDamage(false,true);
			break;
		case "Crit. Sneak Damage":
			selectedWeapon.RollDamage(true, true);
			break;
		}
	}
	
	IEnumerator DisplayWeaponOptions(GameObject target)
	{
		selectedWeapon = target.GetComponent<WeaponListDisplayTemplate>().weapon;
		
		List<string> options = new List<string>{"View Weapon Info","Remove Weapon"};
		
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "Action", false, options)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Weapon Options", descVar));
		
		switch(descVar[0].outVariable)
		{
			case "View Weapon Info":
				WeaponInfo();
				break;
			case "Remove Weapon":
				StartCoroutine(RemoveWeapon());
				break;
		}
	}
	
	void WeaponInfo()
	{
		GameObject g = Instantiate(GameReference.reference.NewWeaponWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		BrowserManager.currentNewTransform = g.transform;
		g.transform.localPosition = BrowserManager.CurrentIndex;
		
		WeaponEditorMenu w = g.GetComponent<WeaponEditorMenu>();
		w.target = gameObject;
		w.functionName = "Do Nothing";
		w.LoadItemDisplay(selectedWeapon.name, true);
	}
	
	IEnumerator RemoveWeapon()
	{
		List<bool> outBools = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to remove " + selectedWeapon.name + "? This will remove all instance of it from your inventory as well.", outBools));
		
		if(outBools[0])
		{
			if(SheetManager.activeCharacter.weapons.Contains(selectedWeapon))
				SheetManager.activeCharacter.weapons.Remove(selectedWeapon);
			
			ToggleWeaponDisplays();
			ToggleWeaponDisplays();
			
			SheetManager.SaveActiveCharacter();
		}
	}
}
